package com.example.mybatisify.healper;

import com.example.mybatisify.entities.Document;
import org.ansj.lucene9.AnsjAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class LuceneHelper {
    private LuceneHelper() {

    }

    private static String indexPath = "/src/main/resources/index";
    private static Analyzer analyzer = new AnsjAnalyzer(AnsjAnalyzer.TYPE.index_ansj);
    private static IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);

    public static void constructIndex(List<Document> documents, boolean erase) throws IOException {
        File indexFile = new File(System.getProperty("user.dir")+indexPath);
        Directory indexDirectory = FSDirectory.open(Paths.get(indexFile.getAbsolutePath()));

        IndexWriter indexWriter = new IndexWriter(indexDirectory, indexWriterConfig);
        if (erase){
            indexWriter.deleteAll();
        }
        for (Document doc : documents) {
            org.apache.lucene.document.Document luceneDocument = new org.apache.lucene.document.Document();
            luceneDocument.add(new TextField("id", doc.getId().toString(), org.apache.lucene.document.Field.Store.YES));
            luceneDocument.add(new TextField("rawText", doc.getRawText(), org.apache.lucene.document.Field.Store.YES));
            indexWriter.addDocument(luceneDocument);
        }
        indexWriter.close();
        System.out.println("Index Update Complete");
    }

    public static List<Document> queryIndex(String field, String queryText) throws IOException{
        File indexFile = new File(System.getProperty("user.dir") + indexPath);
        Directory directory = FSDirectory.open(Paths.get(indexFile.getAbsolutePath()));

        IndexReader indexReader = DirectoryReader.open(directory);
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        // create QueryTerm for the indexing query
        Query luceneQuery = new TermQuery(new Term(field, queryText));
        TopDocs topDocs = indexSearcher.search(luceneQuery, Integer.MAX_VALUE);

        int totalMatches = (int)topDocs.totalHits.value;
        // document scores
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        List<Document> returnDocs = new ArrayList<>();

        // traver the interesting
        for (ScoreDoc scoreDoc : scoreDocs) {
            org.apache.lucene.document.Document document = indexSearcher.doc(scoreDoc.doc);
            // System.out.println("Document ID: " + document.get("id"));
            // System.out.println("Document Content: " + document.get("content"));
            Document doc = new Document();
            doc.setId(Integer.parseInt(document.get("id")));
            doc.setRawText(document.get("rawText"));
        }
        indexReader.close();
        return returnDocs;
    }
}
